home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 7 / Example 7.2 / app.cpp next >
Encoding:
C/C++ Source or Header  |  2006-08-01  |  6.7 KB  |  253 lines

  1. //////////////////////////////////////////////////////////////
  2. // Example 7.2: Animating the Bones                            //
  3. // Written by: C. Granberg, 2006                            //
  4. //////////////////////////////////////////////////////////////
  5.  
  6. #include <windows.h>
  7. #include <d3dx9.h>
  8. #include "debug.h"
  9. #include "skinnedMesh.h"
  10.  
  11. class APPLICATION
  12. {
  13.     public:
  14.         APPLICATION();
  15.         HRESULT Init(HINSTANCE hInstance, int width, int height, bool windowed);
  16.         HRESULT Update(float deltaTime);
  17.         HRESULT Render();
  18.         HRESULT Cleanup();
  19.         HRESULT Quit();
  20.         DWORD FtoDword(float f){return *((DWORD*)&f);}
  21.  
  22.     private:
  23.         IDirect3DDevice9* m_pDevice; 
  24.         SKINNEDMESH m_skinnedMesh;
  25.  
  26.         float m_angle, m_time;
  27.         std::vector<std::string> m_animations;
  28.         int m_activeAnimation;
  29.         HWND m_mainWindow;
  30.         ID3DXFont *m_pFont;
  31. };
  32.  
  33. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd)
  34. {
  35.     APPLICATION app;
  36.  
  37.     if(FAILED(app.Init(hInstance, 800, 600, true)))
  38.         return 0;
  39.  
  40.     MSG msg;
  41.     memset(&msg, 0, sizeof(MSG));
  42.     int startTime = timeGetTime(); 
  43.  
  44.     while(msg.message != WM_QUIT)
  45.     {
  46.         if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  47.         {
  48.             ::TranslateMessage(&msg);
  49.             ::DispatchMessage(&msg);
  50.         }
  51.         else
  52.         {    
  53.             int t = timeGetTime();
  54.             float deltaTime = (t - startTime)*0.001f;
  55.  
  56.             app.Update(deltaTime);
  57.             app.Render();
  58.  
  59.             startTime = t;
  60.         }
  61.     }
  62.  
  63.     app.Cleanup();
  64.  
  65.     return msg.wParam;
  66. }
  67.  
  68. APPLICATION::APPLICATION()
  69. {
  70.     m_pDevice = NULL; 
  71.     m_mainWindow = 0;
  72.     srand(GetTickCount());
  73.     m_angle = m_time = 0.0f;
  74. }
  75.  
  76. HRESULT APPLICATION::Init(HINSTANCE hInstance, int width, int height, bool windowed)
  77. {
  78.     debug.Print("Application initiated");
  79.  
  80.     //Create Window Class
  81.     WNDCLASS wc;
  82.     memset(&wc, 0, sizeof(WNDCLASS));
  83.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  84.     wc.lpfnWndProc   = (WNDPROC)::DefWindowProc; 
  85.     wc.hInstance     = hInstance;
  86.     wc.lpszClassName = "D3DWND";
  87.  
  88.     //Register Class and Create new Window
  89.     RegisterClass(&wc);
  90.     m_mainWindow = CreateWindow("D3DWND", "Example 7.2: Animating the Bones", WS_EX_TOPMOST, 0, 0, width, height, 0, 0, hInstance, 0); 
  91.     SetCursor(NULL);
  92.     ShowWindow(m_mainWindow, SW_SHOW);
  93.     UpdateWindow(m_mainWindow);
  94.  
  95.     //Create IDirect3D9 Interface
  96.     IDirect3D9* d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
  97.  
  98.     if(d3d9 == NULL)
  99.     {
  100.         debug.Print("Direct3DCreate9() - FAILED");
  101.         return E_FAIL;
  102.     }
  103.  
  104.     //Check that the Device supports what we need from it
  105.     D3DCAPS9 caps;
  106.     d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
  107.  
  108.     //Hardware Vertex Processing or not?
  109.     int vp = 0;
  110.     if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
  111.         vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
  112.     else vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  113.  
  114.     //Check vertex & pixelshader versions
  115.     if(caps.VertexShaderVersion < D3DVS_VERSION(2, 0) || caps.PixelShaderVersion < D3DPS_VERSION(2, 0))
  116.     {
  117.         debug.Print("Warning - Your graphic card does not support vertex and pixelshaders version 2.0");
  118.     }
  119.  
  120.     //Set D3DPRESENT_PARAMETERS
  121.     D3DPRESENT_PARAMETERS d3dpp;
  122.     d3dpp.BackBufferWidth            = width;
  123.     d3dpp.BackBufferHeight           = height;
  124.     d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;
  125.     d3dpp.BackBufferCount            = 1;
  126.     d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;
  127.     d3dpp.MultiSampleQuality         = 0;
  128.     d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD; 
  129.     d3dpp.hDeviceWindow              = m_mainWindow;
  130.     d3dpp.Windowed                   = windowed;
  131.     d3dpp.EnableAutoDepthStencil     = true; 
  132.     d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
  133.     d3dpp.Flags                      = 0;
  134.     d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
  135.     d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;
  136.  
  137.     //Create the IDirect3DDevice9
  138.     if(FAILED(d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_mainWindow,
  139.                                  vp, &d3dpp, &m_pDevice)))
  140.     {
  141.         debug.Print("Failed to create IDirect3DDevice9");
  142.         return E_FAIL;
  143.     }
  144.  
  145.     //Release IDirect3D9 interface
  146.     d3d9->Release();
  147.  
  148.     D3DXCreateFont(m_pDevice, 18, 0, 0, 1, false,  
  149.                    DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
  150.                    DEFAULT_PITCH | FF_DONTCARE, "Arial", &m_pFont);
  151.  
  152.     //Load Skinned mesh
  153.     m_skinnedMesh.Load("mesh/drone.x", m_pDevice);    
  154.     m_animations = m_skinnedMesh.GetAnimations();
  155.     m_activeAnimation = m_animations.size() - 3;
  156.     m_skinnedMesh.SetAnimation((char*)m_animations[m_activeAnimation].c_str());
  157.  
  158.     //Set sampler state
  159.     for(int i=0;i<4;i++)
  160.     {
  161.         m_pDevice->SetSamplerState(i, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
  162.         m_pDevice->SetSamplerState(i, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
  163.         m_pDevice->SetSamplerState(i, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
  164.     }
  165.  
  166.     return S_OK;
  167. }
  168.  
  169. HRESULT APPLICATION::Update(float deltaTime)
  170. {    
  171.     m_angle += deltaTime * 0.5f;
  172.     if(m_angle > D3DX_PI * 2.0f)
  173.         m_angle -= D3DX_PI * 2.0f;
  174.  
  175.     m_time = deltaTime * 0.5f;
  176.         
  177.     if(KEYDOWN(VK_SPACE))
  178.     {
  179.         m_activeAnimation++;
  180.         if(m_activeAnimation >= m_animations.size())
  181.             m_activeAnimation = 0;
  182.  
  183.         m_skinnedMesh.SetAnimation((char*)m_animations[m_activeAnimation].c_str());
  184.         Sleep(300);
  185.     }
  186.     else if(KEYDOWN(VK_ESCAPE))
  187.     {
  188.         Quit();
  189.     }
  190.  
  191.     return S_OK;
  192. }    
  193.  
  194. HRESULT APPLICATION::Render()
  195. {
  196.     // Clear the viewport
  197.     m_pDevice->Clear( 0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0L );
  198.  
  199.     //Set camera
  200.     D3DXMATRIX view, proj, world;
  201.     D3DXMatrixLookAtLH(&view, &D3DXVECTOR3(0.0f, 10.0f, -50.0f), &D3DXVECTOR3(0.0f, 4.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
  202.     D3DXMatrixOrthoLH(&proj, 10.0f, 9.0f, 0.1f, 1000.0f);
  203.     
  204.     //Set Skeleton to rotate around the Y-axis
  205.     D3DXMATRIX r, s;
  206.     D3DXMatrixRotationYawPitchRoll(&r, m_angle, 0.0f, 0.0f);
  207.     D3DXMatrixScaling(&s, 1.2f, 1.2f, 1.2f);
  208.     world = s * r;
  209.     
  210.     m_pDevice->SetTransform(D3DTS_VIEW, &view);
  211.     m_pDevice->SetTransform(D3DTS_PROJECTION, &proj);
  212.     m_pDevice->SetTransform(D3DTS_WORLD, &world);
  213.  
  214.     // Begin the scene 
  215.     if(SUCCEEDED(m_pDevice->BeginScene()))
  216.     {
  217.         //Render Skeleton
  218.         m_skinnedMesh.SetPose(world, NULL, m_time);
  219.         m_skinnedMesh.RenderSkeleton(NULL, NULL, world);
  220.  
  221.         RECT r[] = {{10, 10, 0, 0}, {10, 50, 0, 0}};
  222.         m_pFont->DrawText(NULL, "Space: Change Animation", -1, &r[0], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
  223.         std::string str = "Active Animation: " + m_animations[m_activeAnimation];
  224.         m_pFont->DrawText(NULL, str.c_str(), -1, &r[1], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
  225.  
  226.         // End the scene.
  227.         m_pDevice->EndScene();
  228.         m_pDevice->Present(0, 0, 0, 0);
  229.     }
  230.  
  231.     return S_OK;
  232. }
  233.  
  234. HRESULT APPLICATION::Cleanup()
  235. {
  236.     try
  237.     {
  238.         m_pFont->Release();
  239.         m_pDevice->Release();
  240.  
  241.         debug.Print("Application terminated");
  242.     }
  243.     catch(...){}
  244.  
  245.     return S_OK;
  246. }
  247.  
  248. HRESULT APPLICATION::Quit()
  249. {
  250.     ::DestroyWindow(m_mainWindow);
  251.     ::PostQuitMessage(0);
  252.     return S_OK;
  253. }